<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="showAdd = true"
          >添加分类</el-button
        >
      </div>
      <!-- 分类列表 -->
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <el-button type="primary" size="mini">修改</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </el-table-column>
      </el-table>
      <!-- 添加分类弹层 -->
      <el-dialog
        title="添加分类"
        :visible.sync="showAdd"
        width="30%"
        @close="close"
      >
        <!-- 添加的表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addRef"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input
              v-model="addForm.cate_name"
              minlength="1"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input
              v-model="addForm.cate_alias"
              minlength="1"
              maxlength="15"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="showAdd = false">取消</el-button>
          <el-button type="primary" @click="addCate">添加</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>

import { getCateApi, addCateApi } from '@/api/article'
export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章分类数据
      cateList: [],
      showAdd: false,
      // 添加表单数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取文章分类数据
    async getCateList () {
      const { data } = await getCateApi()
      console.log('文章列表数据', data)
      this.cateList = data.data
    },
    // 添加分类
    addCate () {
      this.$refs.addRef.validate(async (isOk) => {
        if (isOk) {
          await addCateApi(this.addForm)
          this.$message.success('添加分类成功')
          this.getCateList()
          this.showAdd = false
        }
      })
    },
    // 关闭弹层做什么
    close () {
      this.$refs.addRef.resetFields()
    }

  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
